package com.itheima.service;

import com.itheima.dao.BaseTeacherDao;
import com.itheima.domain.Option;
import com.itheima.domain.Teacher;
import com.itheima.factory.TeacherDaoFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.Socket;
import java.util.List;

public class TeacherService implements Runnable {
    private static final Logger LOGGER = LoggerFactory.getLogger(TeacherService.class);

    private Socket socket;
    private BaseTeacherDao dao = TeacherDaoFactory.getTeacherDao();

    public TeacherService(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            //接收用户端传过来的报文数据
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String allMsg = br.readLine();
            LOGGER.info("收到来自客户端" + socket.getLocalAddress() + "的报文数据: " + allMsg);
            //解析报文数据
            String[] split = allMsg.split("&");
            Option option = Enum.valueOf(Option.class,split[0]);
            String optionMsg = split[1];
            switch (option){
                case ADD_TEACHER:
                  addTeacher(optionMsg);
                  break;
                case DELECT_TEACHER:
                    delectTeacher(optionMsg);
                    break;
                case UPDATE_TEACHER:
                    updateTeacher(optionMsg);
                    break;
                case FINDALL_TEACHER:
                    findAllTeacher(optionMsg);
                    break;
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void findAllTeacher(String optionMsg) {
        LOGGER.info("查看教师信息.");
        //调用dao层的findAll方法
        List<Teacher> allTeacher = dao.findAllTeacher();
        try {
            //创建序列化流
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            //将list集合写入流
            oos.writeObject(allTeacher);
            //关流释放资源
            oos.close();
            socket.close();
            LOGGER.info("查看完成响应回客户端的数据:" + allTeacher);
        } catch (IOException e) {
            //e.printStackTrace();
            LOGGER.error("查看过程中,服务器出现异常:" + e.getMessage());
        }

    }

    private void updateTeacher(String optionMsg) {
        LOGGER.info("修改教师信息,数据为: " + optionMsg);
        //解析报文,并封装成teacher对象
        String[] split = optionMsg.split(",");
        Teacher teacher = new Teacher(split[0],split[1],split[2],split[3]);
        //调用dao层方法
        boolean res = dao.updateTeacher(split[0], teacher);
        try {
            //将结果响应回客户端
            String resultInfo = res ? "修改成功" : "修改失败!该id不存在,请先添加";
            //创建字符缓冲流
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write(resultInfo);
            bw.flush();
            bw.close();
            socket.close();
            LOGGER.info("修改完成,响应回客户端数据:" + resultInfo);
        } catch (IOException e) {
            //e.printStackTrace();
            LOGGER.error("修改中,服务器出现异常:" + e.getMessage());
        }

    }

    private void delectTeacher(String optionMsg) {
        LOGGER.info("删除教师信息,数据为: " + optionMsg);
        //调用dao层方法,完成删除操作
        boolean res = dao.deleteTeacher(optionMsg);
        try {
            //将结果响应会客户端
            String resultInfo = res ? "删除成功" : "删除失败!该id不存在,无需删除";
            //创建字符缓冲流
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write(resultInfo);
            bw.flush();
            bw.close();
            socket.close();
            LOGGER.info("删除完成,响应回客户端数据:" + resultInfo);
        } catch (IOException e) {
            //e.printStackTrace();
            LOGGER.error("删除中,服务器出现异常:" + e.getMessage());
        }


    }

    private void addTeacher(String optionMsg) {
        LOGGER.info("添加教师信息,数据为: " + optionMsg);
        //解析报文,并封装成teacher对象
        String[] split = optionMsg.split(",");
        Teacher teacher = new Teacher(split[0],split[1],split[2],split[3]);
        //调用dao层方法
        boolean res = dao.addTeacher(teacher);
        try {
            //将结果响应回客户端
            String resultInfo = res ? "添加成功" : "添加失败!该工号已存在,请勿重复添加";
            //创建字符缓冲流
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write(resultInfo);
            bw.flush();
            bw.close();
            socket.close();
            LOGGER.info("添加完成,响应回客户端数据:" + resultInfo);
        } catch (IOException e) {
            //e.printStackTrace();
            LOGGER.error("添加中,服务器出现异常:" + e.getMessage());
        }
    }


}
